package com.minidota.settings;

import java.io.*;
import java.util.*;
import java.util.regex.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author custom
 */
public class Settings {
    private final Properties settings = new Properties();
    private static Settings instance = null;
    public final static String SERVER_PATH = (new File("x")).getAbsolutePath().replaceFirst("x$", "");
    
    public static Settings getInstance() {
        if (instance == null) {
            instance = new Settings();
        }
        return instance;
    }

    private Settings() {
        /*File f = new File("default.properties");
        String res = f.getAbsolutePath();*/
        String res = SERVER_PATH + "default.properties";
        try {
            //settings.load(new InputStreamReader(getClass().getResourceAsStream(res), "utf8"));
            settings.load(new FileInputStream(res));
        } catch (UnsupportedEncodingException e) {
            System.out.println("UnsupportedEncodingException: " + e);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOException: " + e);
            e.printStackTrace();
        }
    }

    public static String getString(String key) {
        try {
            return getInstance().settings.getProperty(key);
        } catch (MissingResourceException e) {
            System.out.println("MissingResourceException: " + e);
            return null;
        }
    }
    
    public static int getInt(String key) {
        try {
            String value = getInstance().settings.getProperty(key);
            return value.length() != 0 ? Integer.parseInt(value) : 0;
        } catch (MissingResourceException e) {
            System.out.println("MissingResourceException: " + e);
            return 0;
        }
    }

    /**
     * Возвращает строку с заменой части строки внутри тегов {} на соответствующую подстроку
     * @param key
     * @return
     */
    public static String getPreparedString(String key) {
        try {
            String query = getInstance().settings.getProperty(key);
            if (query != null && query.contains("{") && query.contains("}")) {
                Pattern p = Pattern.compile("(\\{[^}]+\\})");
                Matcher m = p.matcher(query);
                int i = 0;
                while (m.find()) {
                    ++i;
                    String k = m.group();
                    k = k.substring(1, k.length() - 1);
                    String v = getPreparedString(k);
//                    log.info("{} k '{}'=\n'{}' ", i, k, v);
                    if (query == null) {
                        System.out.println("key " + key + " not found");
                    } else {
                        query = query.replaceFirst("\\{" + k + "\\}", v);
                    }
//                    log.info("query replaced:\n {}", query);
                }
            }
//            log.info("query: {}",query);
            return query;
        } catch (MissingResourceException e) {
            System.out.println("MissingResourceException: " + e);
            return null;
        }
    }
}
